package com.cxrj.zhsq.civilianManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.civilianManage.dao.RdPhoneMapper;
import com.cxrj.zhsq.civilianManage.dao.RelevantDepartmentsMapper;
import com.cxrj.zhsq.civilianManage.entity.RdPhone;
import com.cxrj.zhsq.civilianManage.entity.RelevantDepartments;
import com.cxrj.zhsq.civilianManage.service.RelevantDepartmentsService;
import com.cxrj.zhsq.utils.SqlCondition;

@Service
public class RelevantDepartmentsServiceImpl implements RelevantDepartmentsService {
	@Resource
	private RelevantDepartmentsMapper relevantDepartmentsMapper;
	@Resource
	private RdPhoneMapper rdPhoneMappe; 

	@Override
	public Map<String, Object> selectRdPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> sqlMap = SqlCondition.getBaseCondition(request, response);
		int count = relevantDepartmentsMapper.selectCount(sqlMap);
		List<RelevantDepartments> list = relevantDepartmentsMapper.selectRdPage(sqlMap);
		resultMap.put("total", count);
		resultMap.put("recordsFiltered", count);
		resultMap.put("rows", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertRd(RelevantDepartments relevantDepartments, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		relevantDepartments.setRdId(UUID.randomUUID().toString());
		relevantDepartments.setCreateTime(new Date());
		int a=relevantDepartmentsMapper.insertSelective(relevantDepartments);
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectById(String rdId, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		RelevantDepartments relevantDepartments=relevantDepartmentsMapper.selectByPrimaryKey(rdId);
		resultMap.put("rd", relevantDepartments);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateRd(RelevantDepartments relevantDepartments, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		int a=relevantDepartmentsMapper.updateByPrimaryKeySelective(relevantDepartments);
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertTel(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		String rdId=request.getParameter("rdId");
		String tUuit=request.getParameter("tUuit");
		String tTel=request.getParameter("tTel");
		String tAddress=request.getParameter("tAddress");
		String[] tUuits=tUuit.split(",");
		String[] tTels=tTel.split(",");
		String[] tAddresss=tAddress.split(",");
		Date date = new Date();
		int a=0;
		rdPhoneMappe.deleteByRdId(rdId);
		if(tUuits.length>0){
			for(int i=0;i<tUuits.length;i++){
				RdPhone rdPhone=new RdPhone();
				rdPhone.setRdId(rdId);
				rdPhone.setCreateTime(date);
				rdPhone.settId(UUID.randomUUID().toString());
				rdPhone.settUuit(tUuits[i]);
				rdPhone.settTel(tTels[i]);
				rdPhone.settAddress(tAddresss[i]);
				a=rdPhoneMappe.insertSelective(rdPhone);
			}
			resultMap.put("result", a);
		}
		//resultMap.put("result", 0);
		
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteRd(String rdId) {
		Map<String, Object> resultMap=new HashMap<>();
		int a=relevantDepartmentsMapper.deleteByPrimaryKey(rdId);
		rdPhoneMappe.deleteByRdId(rdId);
		resultMap.put("result", a);
		
		return resultMap;
	}

	@Override
	public Map<String, Object> selectRd(String code, String dataIndex, String pageCount) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("code", code);
		sqlMap.put("start", Integer.parseInt(dataIndex));
		sqlMap.put("length", Integer.parseInt(pageCount));
		List<RelevantDepartments> list = relevantDepartmentsMapper.selectRd(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectRdById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("relevant", relevantDepartmentsMapper.selectByPrimaryKey(id));
		resultMap.put("list", rdPhoneMappe.selectByRdId(id));
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteTelById(String tid) {
		Map<String, Object> resultMap=new HashMap<>();
		int a=rdPhoneMappe.deleteByPrimaryKey(tid);
		resultMap.put("reult", a);
		return resultMap;
	}

}
